/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.mediatek.datatransfer.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.mediatek.datatransfer.R;

public class ViewPagerTabStrip extends LinearLayout {
	private static final String TAG = "ViewPagerTabStrip";
    private int mSelectedUnderlineThickness;
    private final Paint mSelectedUnderlinePaint;

    private int mIndexForSelection;
    private float mSelectionOffset;
    //add by qiankun.gong for OS Contacts
    private int mStartX;
    private int mBottomPointWidth;
    private int mBottomPointMaxWidth;
    private int mCurrentPosition;

    public ViewPagerTabStrip(Context context) {
        this(context, null);
    }

    public ViewPagerTabStrip(Context context, AttributeSet attrs) {
        super(context, attrs);

        final Resources res = context.getResources();

        mSelectedUnderlineThickness =
                res.getDimensionPixelSize(R.dimen.tab_selected_underline_height);
        int underlineColor = res.getColor(R.color.tab_selected_underline_color);
        int backgroundColor = res.getColor(R.color.actionbar_background_color);

        mSelectedUnderlinePaint = new Paint();
        mSelectedUnderlinePaint.setColor(underlineColor);

        setBackgroundColor(backgroundColor);
        setWillNotDraw(false);
        
        //add by qiankun.gong 
        mSelectedUnderlinePaint.setAntiAlias(true);
        mBottomPointWidth = res.getDimensionPixelSize(R.dimen.tab_bottom_point_width);
        mBottomPointMaxWidth = res.getDimensionPixelSize(R.dimen.tab_bottom_point_max_width);
        this.setPadding(100, 0, 100, 0);
    }

    /**
     * Notifies this view that view pager has been scrolled. We save the tab index
     * and selection offset for interpolating the position and width of selection
     * underline.
     */
    void onPageScrolled(int position, float positionOffset, int positionOffsetPixels,int currentPosition) {
        mIndexForSelection = position;
        mSelectionOffset = positionOffset;
        //add by qiankun.gong for OS Contacts
        mCurrentPosition = currentPosition;
        invalidate();
    }
    //add by qiankun.gong for OS Contacts start
    public void setUnderlinePaintColor(int color){
    	if(mSelectedUnderlinePaint != null){
    		mSelectedUnderlinePaint.setColor(color);
    		invalidate();
    	}
    }
   //add by qiankun.gong for OS Contacts end
    @Override
    protected void onDraw(Canvas canvas) {
        int childCount = getChildCount();
        //add by qiankun.gong for OS Contacts
        int selectWidth = 0;
        int nextWidth = 0;
        int nextLeft = 0;
        int nextRight = 0;
        View selectedTitle = null;
        View nextTitle = null;
        // Thick colored underline below the current selection
        if (childCount > 0) {
        	 //modify by qiankun.gong for OS Contacts
            selectedTitle = getChildAt(mIndexForSelection);
            if(selectedTitle == null){
            	return;
            }
            int selectedLeft = selectedTitle.getLeft();
            int selectedRight = selectedTitle.getRight();
           //add by qiankun.gong for OS Contacts
            selectWidth = selectedTitle.getWidth();
            Log.d(TAG,"selectWidth = " + selectWidth);
            final boolean isRtl = isRtl();
            final boolean hasNextTab = isRtl ? mIndexForSelection > 0
                    : (mIndexForSelection < (getChildCount() - 1));
            if ((mSelectionOffset > 0.0f) && hasNextTab) {
                // Draw the selection partway between the tabs
                 nextTitle = getChildAt(mIndexForSelection + (isRtl ? -1 : 1));
                 if(nextTitle == null){
                	 return;
                 }
                nextLeft = nextTitle.getLeft();
                nextRight = nextTitle.getRight();
                /*
                selectedLeft = (int) (mSelectionOffset * nextLeft +
                        (1.0f - mSelectionOffset) * selectedLeft);
                selectedRight = (int) (mSelectionOffset * nextRight +
                        (1.0f - mSelectionOffset) * selectedRight);
                        */
               //add by qiankun.gong for OS Contacts
                nextWidth = nextTitle.getWidth();
                Log.d(TAG,"nextWidth = " + nextWidth);
            }
            int height = getHeight();
            //add by qiankun.gong for OS Contact start
            int width = getWidth();
            canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
            if(mSelectionOffset == 0){
            	mStartX = selectedLeft+selectedTitle.getWidth()/2;
                canvas.drawCircle(selectedLeft+selectedTitle.getWidth()/2, height - mBottomPointWidth*2, mBottomPointWidth/2, mSelectedUnderlinePaint);
            }else{
            	Log.d(TAG,"mStartX = " + mStartX);
            	Log.d(TAG,"selectedRight = " + selectedRight);
            	Log.d(TAG,"selectedLeft = " + selectedLeft);
            	Log.d(TAG,"selectedTitle.getWidth = " + selectedTitle.getWidth());
            	RectF mRect = new RectF();
            	mRect.top = height -mBottomPointWidth*2-mBottomPointWidth/2;
            	mRect.bottom = height -mBottomPointWidth*2+mBottomPointWidth/2;
            	
                if(mCurrentPosition == 0 && mIndexForSelection == 0){ //0-1
                	selectedTitle = getChildAt(0);
                	nextTitle = getChildAt(1);
                	if(selectedTitle == null || nextTitle == null){
                		return;
                	}
                	selectWidth = selectedTitle.getWidth();
                	nextWidth = nextTitle.getWidth();
                	
                	mRect.left = selectedTitle.getLeft()+selectedTitle.getWidth()/2 - mBottomPointWidth/2;
                	mRect.right = mRect.left + mBottomPointWidth + mSelectionOffset * ((selectWidth + nextWidth)/2);
                	Log.d(TAG,"mRect.right = " + mRect.right);
                	Log.d(TAG,"mRect.left = " + mRect.left);
                	Log.d(TAG,"mSelectionOffset * ((selectWidth + nextWidth))/2 = " + mSelectionOffset * ((selectWidth + nextWidth)/2));
                	if(mRect.right + mBottomPointMaxWidth < nextLeft + nextWidth/2- mBottomPointWidth/2){
	                	if(mRect.right - mRect.left >= mBottomPointMaxWidth){
	                   		mRect.left = mRect.left + mSelectionOffset * ((selectWidth + nextWidth)/2) -mBottomPointWidth*2;
	                		mRect.right = mRect.left + mBottomPointMaxWidth;
	                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
	                	}else{
	                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
	                	}
                	}else{
                		mRect.left = mRect.left + mSelectionOffset * ((selectWidth + nextWidth)/2) - mBottomPointWidth*2;
                		if(mRect.left >= nextLeft + nextWidth/2 - mBottomPointWidth/2){
                			mRect.left = nextLeft + nextWidth/2 - mBottomPointWidth/2;
                		}
                		if(mRect.right >= nextLeft + nextWidth/2 + mBottomPointWidth/2){
                			mRect.right = nextLeft + nextWidth/2 + mBottomPointWidth/2;
                		}
                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
                	}
                }else if(mCurrentPosition == 1 && mIndexForSelection == 0){ //1-0
                	
                	selectedTitle = getChildAt(1);
                	nextTitle = getChildAt(0);
                	if(selectedTitle == null || nextTitle == null){
                		return;
                	}
                	selectWidth = selectedTitle.getWidth();
                	nextWidth = nextTitle.getWidth();
                	
                	mRect.left = selectedTitle.getLeft()+selectedTitle.getWidth()/2- mBottomPointWidth/2 -(1- mSelectionOffset) * ((selectWidth + nextWidth)/2);
                	mRect.right = selectedTitle.getLeft() + selectedTitle.getWidth()/2+mBottomPointWidth/2;
                   	if(mRect.right - mRect.left >= mBottomPointMaxWidth){
                		mRect.right =mRect.right -(1- mSelectionOffset) * ((selectWidth + nextWidth)/2) + mBottomPointWidth*2;
                   		mRect.left = mRect.right - mBottomPointMaxWidth;
                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
                	}else{
                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
                	}	
                }else if(mCurrentPosition == 1 && mIndexForSelection == 1){ //1-2
                	
                	selectedTitle = getChildAt(1);
                	nextTitle = getChildAt(2);
                	if(selectedTitle == null || nextTitle == null){
                		return;
                	}
                	selectWidth = selectedTitle.getWidth();
                	nextWidth = nextTitle.getWidth();
                	
                	mRect.left = selectedTitle.getLeft()+selectedTitle.getWidth()/2 - mBottomPointWidth/2;
                	mRect.right = mRect.left + mBottomPointWidth + mSelectionOffset * ((selectWidth + nextWidth)/2);
                	if(mRect.right + mBottomPointMaxWidth < nextLeft + nextWidth/2- mBottomPointWidth/2){
	                	if(mRect.right - mRect.left >= mBottomPointMaxWidth){
	                   		mRect.left = mRect.left + mSelectionOffset * ((selectWidth + nextWidth)/2) -mBottomPointWidth*2;
	                		mRect.right = mRect.left + mBottomPointMaxWidth;
	                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
	                	}else{
	                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
	                	}
                	}else{
                		mRect.left = mRect.left + mSelectionOffset * ((selectWidth + nextWidth)/2) - mBottomPointWidth*2;
                		if(mRect.left >= nextLeft + nextWidth/2 - mBottomPointWidth/2){
                			mRect.left = nextLeft + nextWidth/2 - mBottomPointWidth/2;
                		}
                		if(mRect.right >= nextLeft + nextWidth/2 + mBottomPointWidth/2){
                			mRect.right = nextLeft + nextWidth/2 + mBottomPointWidth/2;
                		}
                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
                	}
                }else if(mCurrentPosition == 2 && mIndexForSelection == 1){ //2-1
                 	selectedTitle = getChildAt(2);
                	nextTitle = getChildAt(1);
                	if(selectedTitle == null || nextTitle == null){
                		return;
                	}
                	selectWidth = selectedTitle.getWidth();
                	nextWidth = nextTitle.getWidth();
                	
                	mRect.left = selectedTitle.getLeft()+selectedTitle.getWidth()/2- mBottomPointWidth/2 -(1- mSelectionOffset) * ((selectWidth + nextWidth)/2);
                	mRect.right = selectedTitle.getLeft() + selectedTitle.getWidth()/2+mBottomPointWidth/2;
                	Log.d(TAG,"mRect.left = " + mRect.left + "mRect.right = " + mRect.right);
                   	if(mRect.right - mRect.left >= mBottomPointMaxWidth){
                		mRect.right =mRect.right -(1- mSelectionOffset) * ((selectWidth + nextWidth)/2) + mBottomPointWidth*2;
                   		mRect.left = mRect.right - mBottomPointMaxWidth;
                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
                	}else{
                		canvas.drawRoundRect(mRect,mBottomPointWidth, mBottomPointWidth, mSelectedUnderlinePaint);
                	}		
                }
            }
           //add by qiankun.gong for OS Contact end
        }
    }

    private boolean isRtl() {
    	//delete by qiankun.gong for OS Contacts
       // return getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
    	return false;
    }
}